package com.csu.mall.product.app;

import com.csu.mall.common.dto.BrandTo;
import com.csu.mall.common.utils.PageUtils;
import com.csu.mall.common.utils.R;
import com.csu.mall.product.entity.BrandEntity;
import com.csu.mall.product.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 品牌
 *
 * @author zhenhuanli
 * @email 743879362@qq.com
 * @date 2022-09-16 11:42:54
 */
@RestController
@Slf4j
@RequestMapping("product/brand")
public class BrandController {
    @Resource
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        log.info("page: {}", page);
        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody BrandEntity brand){
		brandService.save(brand);

        return R.ok();
    }

    @GetMapping("/info")
    public List<BrandTo> brandInfo(@RequestParam List<Long> brandIds){
        return brandService.brandsInfo(brandIds);
    }
    /**
     * 修改品牌
     * @Valid 开启参数校验
     */
    @RequestMapping("/update")
    public R update(@Valid @RequestBody BrandEntity brand){
//		brandService.updateById(brand);
        brandService.updateEntity(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }


}
